package com.jyniubi.gmall.oms.listener;

import com.jyniubi.gmall.oms.mapper.OrderMapper;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
public class OrderListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private OrderMapper orderMapper;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("OMS.PAY.QUEUE"),
            exchange = @Exchange(value = "ORDER.EXCHANGE",type = ExchangeTypes.TOPIC,ignoreDeclarationExceptions = "true"),
            key = {"order.pay"}
    ))
    public void payOrder(String orderToken, Channel channel, Message message) throws IOException {
        // orderToken判空
        if (StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        // 将订单的状态改为待发货
        if (this.orderMapper.updateStatus(orderToken,1,0) == 1) {
            //发送消息给wms解锁库存
            this.rabbitTemplate.convertAndSend("ORDER.EXCHANGE","order.minus",orderToken);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("OMS.DISABLE.QUEUE"),
            exchange = @Exchange(value = "ORDER.EXCHANGE",type = ExchangeTypes.TOPIC,ignoreDeclarationExceptions = "true"),
            key = {"order.fail"}
    ))
    public void unLock(String orderToken, Channel channel, Message message) throws IOException {
        // orderToken判空
        if (StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        // 将订单的状态改为无效订单
        this.orderMapper.updateStatus(orderToken,5,0);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }

    @RabbitListener(queues = "ORDER.DEAD.QUEUE")
    public void closeOrder(String orderToken, Channel channel, Message message) throws IOException {
        // orderToken判空
        if (StringUtils.isBlank(orderToken)){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            return;
        }
        // 将订单的状态改为无效订单
        if (this.orderMapper.updateStatus(orderToken,4,0)==1) {
            // 发送消息给wms解锁库存
            this.rabbitTemplate.convertAndSend("ORDER.EXCHANGE","stock.unlock",orderToken);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }
}
